package niit.wei.service.impl;

import niit.wei.dao.BookDao;
import niit.wei.dao.impl.BookDaoImpl;

import niit.wei.dto.Page;
import niit.wei.entity.Book;
import niit.wei.entity.Category;
import niit.wei.service.BookService;
import niit.wei.service.CategoryService;
import niit.wei.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author WeiJinLong
 * @Date 2023-06-28 21:25
 * @Version 1.0
 */
public class BookServiceImpl implements BookService {

    private BookDao bookDao = new BookDaoImpl();
    private CategoryService categoryService = new CategoryServiceImpl();

    @Override
    public void addBook(Book book) {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert  into book (book_name,author_name,price,category_id) values(?,?,?,?)";
            bookDao.addBook(conn, sql, book.getBookName(), book.getAuthorName(), book.getPrice(), book.getCategoryId());
            conn.commit();
        } catch (SQLException | ClassNotFoundException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                JDBCUtils.close();
            }
        }
    }

    @Override
    public List<Book> getAllBooks() throws SQLException, ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        Connection conn = JDBCUtils.getConnection();
        String sql = "select book_id bookId,book_name bookName,author_name authorName,price,category_id categoryId,book_img bookImg  from book";
        List<Book> bookList = bookDao.getBookList(conn, sql, Book.class);
        for (Book book : bookList) {
            Category byId = categoryService.findById(book.getCategoryId());
            book.setCategory(byId);
        }
//        return bookDao.getBookList(conn,sql,Book.class);
        return bookList;
    }

    @Override
    public Book getBookById(int id) {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select book_id bookId,book_name bookName,author_name authorName,price,category_id categoryId,book_img bookImg  from book where book_id =?";
            Book book = bookDao.getBook(conn, sql, Book.class, id);
            return book;
        } catch (SQLException | ClassNotFoundException | NoSuchFieldException | InstantiationException |
                 IllegalAccessException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                JDBCUtils.close();
            }
        }
    }

    @Override
    public void updateBook(Book book) {
        Connection conn = null;
        ;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update book set book_name =?,author_name =?,price =?,category_id =? where book_id =?";
            bookDao.updateBook(conn, sql, book.getBookName(), book.getAuthorName(), book.getPrice(), book.getCategoryId(), book.getBookId());
            conn.commit();
        } catch (SQLException | ClassNotFoundException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                JDBCUtils.close();
            }
        }
    }

    @Override
    public void deleteBook(int id) {
        Connection conn = null;
        ;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "delete from book where book_id =?";
            bookDao.deleteBook(conn, sql, id);
            conn.commit();
        } catch (SQLException | ClassNotFoundException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                JDBCUtils.close();
            }
        }
    }


    @Override
    public Page getBookPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        try {
            Connection conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "select book_id bookId,book_name bookName,author_name authorName,price,category_id categoryId  from book limit  ?,?";
            List<Book> bookList = bookDao.getBookList(conn, sql, Book.class, (pageNo - 1) * pageSize, pageSize);
            for (Book book : bookList) {
                Integer categoryId = book.getCategoryId();
                Category byId = categoryService.findById(categoryId);
                book.setCategory(byId);
            }
            page.setPage(bookList);
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            //获取Book表的总记录数
            Long bookCount = bookDao.getBookCount(conn, "select count(*) count from book");
            page.setCount(bookCount);
            //获取总页数
            long count = (bookCount % pageSize == 0) ? (bookCount / pageSize) : (bookCount / pageSize + 1);
            int PageCount = (int) count;
            page.setPageCount(PageCount);

            page.setHasPrePage(pageNo <= 1 ? false : true);
            page.setHasNextPage(pageNo >= PageCount ? false : true);

            conn.commit();
        } catch (SQLException | NoSuchFieldException | InstantiationException | IllegalAccessException |
                 ClassNotFoundException e) {
            throw new RuntimeException(e);

        }
        return page;
    }
}
